home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / lib2to3 / fixes / fix_isinstance.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  1.8 KB  |  48 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''Fixer that cleans up a tuple argument to isinstance after the tokens
  5. in it were fixed.  This is mainly used to remove double occurrences of
  6. tokens as a leftover of the long -> int / unicode -> str conversion.
  7.  
  8. eg.  isinstance(x, (int, long)) -> isinstance(x, (int, int))
  9.        -> isinstance(x, int)
  10. '''
  11. from  import fixer_base
  12. from fixer_util import token
  13.  
  14. class FixIsinstance(fixer_base.BaseFix):
  15.     PATTERN = "\n    power<\n        'isinstance'\n        trailer< '(' arglist< any ',' atom< '('\n            args=testlist_gexp< any+ >\n        ')' > > ')' >\n    >\n    "
  16.     run_order = 6
  17.     
  18.     def transform(self, node, results):
  19.         names_inserted = set()
  20.         testlist = results['args']
  21.         args = testlist.children
  22.         new_args = []
  23.         iterator = enumerate(args)
  24.         for idx, arg in iterator:
  25.             if arg.type == token.NAME and arg.value in names_inserted:
  26.                 if idx < len(args) - 1 and args[idx + 1].type == token.COMMA:
  27.                     iterator.next()
  28.                     continue
  29.                 
  30.             args[idx + 1].type == token.COMMA
  31.             new_args.append(arg)
  32.             if arg.type == token.NAME:
  33.                 names_inserted.add(arg.value)
  34.                 continue
  35.         
  36.         if new_args and new_args[-1].type == token.COMMA:
  37.             del new_args[-1]
  38.         
  39.         if len(new_args) == 1:
  40.             atom = testlist.parent
  41.             new_args[0].set_prefix(atom.get_prefix())
  42.             atom.replace(new_args[0])
  43.         else:
  44.             args[:] = new_args
  45.             node.changed()
  46.  
  47.  
  48.